﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading;
using BrainTechLLC.ThreadSafeObjects;
using System.Runtime.Serialization;
using System.Reflection;

namespace BrainTechLLC
{
    public static class BasicObjects_RegisterTypes
    {
        public static Type[] RegisterTypes()
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            DLLContents contents = TypeProvider.GetInstance().GetDLLContents(assembly.FullName);
            var types = assembly.GetTypes();
            types.ForEach(type =>
            {
                if (type.IsTypeOkay())
                    TypeProvider.GetInstance().AddType(contents, type);
            });
            return types;
        }
    }

    public class RepeatingTask : IExecutableWorkItem
    {
        public object _lock;
        //public bool Scheduled { get; set; }
        public ActionAndOrderCollection Actions { get; set; }
        public ActionAndOrderCollection OrderedActions;
        public bool Cancelled { get; set; }
        public DateTime ExecutionTime { get; set; }
        public ScheduledTime Schedule { get; set; }

        public RepeatingTask()
        {
        }

        public void VerifyOrderedActions()
        {
            if (_lock == null)
                Interlocked.CompareExchange<object>(ref _lock, new object(), null);

            if (OrderedActions == null)
            {
                lock (_lock)
                {
                    if (OrderedActions == null)
                    {
                        Actions.OrderActions();
                        Interlocked.CompareExchange<ActionAndOrderCollection>(ref OrderedActions, Actions, null);
                    }
                }
            }
        }

        public void Execute()
        {
            DateTime dt = DateTime.MinValue;

            if (Schedule != null && ((Schedule.Options & ScheduleOptions.ScheduleFromStartTime) == ScheduleOptions.ScheduleFromStartTime))
            {
                dt = DateTime.Now;
            }

            if (Actions != null)
            {
                VerifyOrderedActions();

                for (int n = 0; n < OrderedActions.Count; n++)
                {
                    var item = OrderedActions[n];

                    item.Action.MethodDelegate.DynamicInvoke(null);
                }
            }

            if (dt == DateTime.MinValue)
            {
                dt = DateTime.Now;
            }

            if (Schedule != null)
            {
                if (((Schedule.Options & ScheduleOptions.ScheduleAtTime) == ScheduleOptions.ScheduleAtTime &&
                    Schedule.ScheduleTime != DateTime.MinValue))
                {
                    ExecutionTime = dt;
                }
                else if ((Schedule.Options & ScheduleOptions.ScheduleRepeatingMS) == ScheduleOptions.ScheduleRepeatingMS)
                {
                    ExecutionTime = dt.AddMilliseconds(Schedule.DelayMS);
                }

                if (SchedulingEngine.Instance != null)
                    SchedulingEngine.Instance.ScheduleEventToExecute(this);
            }
        }
    }
}